package cn.lanqiao.bigDate.web.controller;

import cn.lanqiao.bigDate.entity.data.DataBook;


import cn.lanqiao.bigDate.reslut.Result;
import cn.lanqiao.bigDate.web.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@RestController
@RequestMapping(DataBookController.BASE_URL)
public class DataBookController extends BaseController {
    public static final String BASE_URL = "dataBook";

    /**
     * lxxing 2022322
     * 页面跳转
     * @return
     */
    @RequestMapping(value = "toIndex")
    public ModelAndView toIndex() {
        ModelAndView mav = new ModelAndView();
        //指定路径
        mav.setViewName(BASE_URL + "/index");
        return mav;
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping(value = "findBookByPage")
    public IPage<DataBook> findBookByPage(Page<DataBook> page ,String searchBookNameAndIsbn,String searchBookFormat ,String searchBookFraming) {
        return dataBookService.findBookByPage(page,searchBookNameAndIsbn,searchBookFormat,searchBookFraming);
    }

    /**
     * 按ID查询
     * @return  结果
     */
    @GetMapping(value = "findBookOne")
    public DataBook findBookOne(Long id) {
        return dataBookService.findBookOne(id);
    }
    /**
     * 模糊匹配name
     * @return  结果
     */
    @GetMapping(value = "findBookName")
    public List<String> findBookName(String name) {
        return dataBookService.findBookName(name);
    }

    /**
     * 按书名查询
     * @return  结果
     */
    @GetMapping(value = "findBookByName")
    public Result<Boolean> findBookByName(String name) {
        Result<Boolean> reslut = new Result();
        Boolean aBoolean = dataBookService.findBookByName(name);
        if(aBoolean){
            reslut.setCode(200);
            reslut.setMsg("true");
        }else{
            reslut.setCode(500);
            reslut.setMsg("false");
        }
        return reslut;
    }
    /**
     * 按isbn查询
     * @return  结果
     */
    @GetMapping(value = "findBookByIsbn")
    public Result<Boolean> findBookByIsbn(String isbn) {
        Result<Boolean> reslut = new Result();
        Boolean aBoolean = dataBookService.findBookByIsbn(isbn);
        if(aBoolean){
            reslut.setCode(200);
            reslut.setMsg("true");
        }else{
            reslut.setCode(500);
            reslut.setMsg("false");
        }
        return reslut;
    }
    /**
     * 按id删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "deleteBookById")
    public Result<String> deleteBookById(String ids) {
        Result<String> reslut = new Result();
        Boolean isSuccess = dataBookService.deleteBookById(ids);
        if(isSuccess){
            reslut.setCode(200);
            reslut.setMsg("删除成功");
        }else{
            reslut.setCode(500);
            reslut.setMsg("删除失败");
        }
        return reslut;
    }



    /**
     * 添加图书信息
     * @param dataBook
     * @return
     */
    @PostMapping(value = "addBookData")
    public Result<String> addBookData(DataBook dataBook) {
        Result<String> reslut = new Result();
        Boolean isSuccess = dataBookService.addBookData(dataBook);
        if(isSuccess){
            reslut.setCode(200);
            reslut.setMsg("添加成功");
        }else{
            reslut.setCode(500);
            reslut.setMsg("添加失败");
        }
        return reslut;
    }


    /**
     * 修改图书信息
     * @param dataBook
     * @return
     */
    @PostMapping(value = "updateBookById")
    public Result<String> updateBookById(DataBook dataBook) {
        Result<String> reslut = new Result();
        Boolean isSuccess = dataBookService.updateBookById(dataBook);
        if(isSuccess){
            reslut.setCode(200);
            reslut.setMsg("修改成功");
        }else{
            reslut.setCode(500);
            reslut.setMsg("修改失败");
        }
        return reslut;
    }
}
